<?php if (!defined('THINK_PATH')) exit(); /*a:2:{s:35:"./themes/admin/dp\article_edit.html";i:1743055608;s:39:"D:\phpcode\syzth\themes\admin\base.html";i:1741049647;}*/ ?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title><?php echo $setup['title']; ?></title>
    <meta content="<?php echo $setup['keywords']; ?>" name="keywords" />
    <meta content="<?php echo $setup['description']; ?>" name="description" />
    <?php if($setup['ico'] != null): ?><link rel="shortcut icon" type="image/x-icon"  href="/public/uploads/<?php echo $setup['ico']; ?>"/><?php endif; ?>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/public/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/public/layuiadmin/style/admin.css" media="all">
    <!--CSS引用-->
    
    <!--页面CSS-->
    <!--[if lt IE 9]>
    <script src="/public/static/js/html5shiv.min.js"></script>
    <script src="/public/static/js/respond.min.js"></script>
    <![endif]-->
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li><a href="<?php echo url('admin/dp/article'); ?>">文章管理</a></li>
                <li class="layui-this">编辑文章</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <form class="layui-form" action="<?php echo url('admin/dp/edit_article'); ?>" method="post">
                        <input type="hidden" name="id" value="<?php echo $record['id']; ?>">
                        <div class="layui-form-item">
                            <label class="layui-form-label">文章标题</label>
                            <div class="layui-input-block">
                                <input type="text" name="title" value="<?php echo $record['title']; ?>" required lay-verify="required" placeholder="请输入标题" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">文章内容</label>
                            <div class="layui-input-block">
                                <div id="editor-wrapper">
                                    <div id="toolbar-container"></div>
                                    <div id="editor-container"></div>
                                </div>
                                <textarea name="content" id="content" style="display:none;"><?php echo $record['content']; ?></textarea>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="submitArticle">提交</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<!--页面-->

<!--JS引用-->
<script src="/public/static/js/jquery.min.js"></script>
<script src="/public/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/public/layuiadmin/' //静态资源所在路径
    }).extend({
        index: "lib/index" //主入口模块
    }).use('index');
</script>

<!-- WangEditor V5 引入 -->

<link href="/public/assets/css/style.css" rel="stylesheet" />
<script src="/public/assets/js/index.js"></script>

<style>
#editor-wrapper {
    border: 1px solid #ccc;
    z-index: 100;
}
#toolbar-container {
    border-bottom: 1px solid #ccc;
}
#editor-container {
    height: 400px;
}
</style>

<script>
layui.use(['form'], function(){
    var form = layui.form;

    const { createEditor, createToolbar } = window.wangEditor;

    const editorConfig = {
        placeholder: '请输入文章内容...',
        onChange(editor) {
            const html = editor.getHtml();
            document.getElementById('content').value = html;
        },
        MENU_CONF: {
            uploadImage: {
                server: '/index/index/upload',  // 图片上传接口
                fieldName: 'file',
                customInsert(res, insertFn) {
                    if(res.code === 1){
                        insertFn(res.data.url);
                    } else {
                        layer.msg('图片上传失败');
                    }
                }
            }
        }
    };

    const editor = createEditor({
        selector: '#editor-container',
        html: `<?php echo html_entity_decode($record['content']); ?>`,
        config: editorConfig,
        mode: 'default',
    });

    const toolbar = createToolbar({
        editor,
        selector: '#toolbar-container',
        mode: 'default',
    });

    // 表单提交
    form.on('submit(submitArticle)', function(data){
        const content = editor.getHtml();
        if(!content || content === '<p><br></p>'){
            layer.msg('文章内容不能为空');
            return false;
        }
        $('#content').val(content);

        $.post(data.form.action, $(data.form).serialize(), function(info){
            if(info.code === 1){
                layer.msg(info.msg);
                setTimeout(function(){
                    location.href = "<?php echo url('admin/dp/article'); ?>";
                }, 1000);
            } else {
                layer.msg(info.msg);
            }
        });

        return false;
    });
});
</script>

<!--页面JS脚本-->
</body>
</html>

